<template>
  <div class="app-container page-container right-side-dialog">
    <!-- 搜索条件 -->
    <div class="header-search single_line">
      <el-form ref="search_form" :model="search" :inline="true" label-width="0">
        <div class="form-area">
          <div class="search_view">
            <el-form-item>
              <el-input v-model="search.app_name" placeholder="请输入应用名称" />
            </el-form-item>
            <el-form-item>
              <el-select v-model="search.user_token" clearable placeholder="登录访问">
                <el-option label="否" :value="0" />
                <el-option label="是" :value="1" />
              </el-select>
            </el-form-item>
            <div class="btn-area">
              <span class="spp-btn spp-btn-primary" @click="getAppPaginateData()">搜索</span>
              <span class="spp-btn" @click="resetData()">重置</span>
            </div>
          </div>
        </div>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="content_view">
      <!-- 操作按鈕 -->
      <div class="main-header">
        <el-row :gutter="20">
          <el-col :span="4" class="left">应用列表</el-col>
          <el-col :span="20" class="right">
            <span v-if="auth.save != -1" class="spp-btn spp-btn-primary" @click="appEditVisible = true">新增</span>
            <span v-if="auth.cache != -1" class="spp-btn" @click="appCache()">同步至缓存</span>
          </el-col>
        </el-row>
      </div>
      <el-table ref="table" :data="app_paginate_data" :height="height-130" size="mini" stripe>
        <el-table-column label="序号" type="index" width="60" />
        <el-table-column label="APP_ID" prop="app_id" min-width="300" align="left" />
        <el-table-column label="应用名称" prop="app_name" min-width="150" align="left" />
        <el-table-column label="过期时间" min-width="200" align="left">
          <template slot-scope="scope">
            <div>{{ scope.row.expire_time ? formatDateTime(scope.row.expire_time) : '--' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" min-width="200" align="left">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time ? formatDateTime(scope.row.create_time) : '--' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="校验身份" min-width="100" align="left">
          <template slot-scope="scope">
            <div>{{ scope.row.user_token ==='1'?'校验':'不校验' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150" fixed="right" align="left">
          <template slot-scope="scope">
            <el-button v-if="auth.update != -1" type="text" @click="appUpdate(scope.row)">修改</el-button>
            <el-button v-if="auth.secret != -1" type="text" @click="getAppSecret(scope.row.app_id)">查看密钥</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="page" :limit.sync="page_size" @pagination="getAppPaginateData" />
    </div>
    <!-- 新增编辑应用 -->
    <el-dialog :title="app_edit_form.title" :visible.sync="appEditVisible" :close-on-click-modal="false" width="720px">
      <el-scrollbar wrap-class="vertical_scrollbar" :style="{height: height + 'px'}">
        <el-form ref="app_edit_form" label-width="20px" :model="app_edit_form">
          <el-row>
            <el-col :span="12">
              <el-form-item prop="app_name">
                <div class="form_lable">应用名称</div>
                <el-input v-model="app_edit_form.app_name" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="user_type">
                <div class="form_lable">授权用户类型</div>
                <el-select v-model="app_edit_form.user_type" multiple placeholder="请选择授权用户类型">
                  <el-option v-for="(value, key, index) in user_type_dic" :key="index" :label="value" :value="key"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="expire_time">
                <div class="form_lable">过期时间</div>
                <el-date-picker v-model="app_edit_expire_time" type="datetime" placeholder="结束录入时间" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="add_secret">
                <div class="form_lable">是否需要密钥加密</div>
                <el-radio-group v-model="app_edit_form.add_secret">
                  <el-radio label="0">否</el-radio>
                  <el-radio label="1">是</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="user_token">
                <div class="form_lable">是否登录访问</div>
                <el-radio-group v-model="app_edit_form.user_token">
                  <el-radio label="0">否</el-radio>
                  <el-radio label="1">是</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="token_expire">
                <div class="form_lable">tocken过期时间/分钟</div>
                <el-input v-model="app_edit_form.token_expire" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="restrict_access">
                <div class="form_lable">限制访问平台</div>
                <el-radio-group v-model="app_edit_form.restrict_access">
                  <el-radio label="0">否</el-radio>
                  <el-radio label="1">是</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="restrict_platform">
                <div class="form_lable">平台代码</div>
                <el-select v-model="app_edit_form.restrict_platform" multiple placeholder="请选择平台代码">
                  <el-option v-for="(value, key, index) in restrict_platform_dic" :key="index" :label="value" :value="key"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-scrollbar>
      <div class="dialog-btn" style="text-align: right">
        <span class="spp-btn" @click="appEditVisible = false">取消</span>
        <span class="spp-btn spp-btn-primary" @click="appEditSubmit()">保存</span>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { paginate, save, update, cache, secret } from '@/api/pajx/sys/app'
import { user_type, restrict_platform } from '@/api/public'
import { formatDateTime, parseTime } from '@/utils/index'
import { mapState } from 'vuex'
import Pagination from '@/components/Pagination'
export default {
  name: 'SysApp',
  components: { Pagination },
  data() {
    return {
      height: window.innerHeight - 135,
      auth: {},
      user_type_dic: {},
      restrict_platform_dic: {},
      search: {
        user_token: '',
        app_name: ''
      },
      page: 1,
      page_size: 20,
      total: 0,
      app_paginate_data: [],
      appEditVisible: false,
      app_edit_expire_time: '',
      app_edit_form: {
        title: '新增应用',
        type: 'add',
        app_name: '',
        user_type: [],
        expire_time: '',
        add_secret: '',
        user_token: '',
        token_expire: '',
        restrict_access: '',
        restrict_platform: []
      }
    }
  },
  computed: {
    ...mapState({
      permission: state => state.permission.permission
    })
  },
  watch: {
    appEditVisible(val) {
      if (val) {
        this.app_edit_form.title = this.app_edit_form.type==='add'?'新增应用':'编辑应用'
      }
      if (!val) {
        this.app_edit_form.app_name = ''
        this.app_edit_form.user_type = []
        this.app_edit_form.expire_time = ''
        this.app_edit_form.add_secret = ''
        this.app_edit_form.user_token = ''
        this.app_edit_form.token_expire = ''
        this.app_edit_form.restrict_access = ''
        this.app_edit_form.restrict_platform = []
        this.app_edit_form.type = 'add'
      }
    }
  },
  methods: {
    formatDateTime: formatDateTime,
    // 获取按钮权限
    permissionAuth() {
      this.auth.save = this.permission.indexOf('/api/sys/app/save')
      this.auth.update = this.permission.indexOf('/api/sys/app/update')
      this.auth.cache = this.permission.indexOf('/api/sys/app/cache')
      this.auth.secret = this.permission.indexOf('/api/sys/app/secret')
    },
    // 重置搜索选项
    resetData() {
      Object.assign(this.$data.search, this.$options.data().search)
    },
    // 获取用户类型字典
    getUserTypeDic() {
      user_type().then(response => {
        this.user_type_dic = response.data
      })
    },
    // 获取平台代码字典
    getRestrictPlatformDic() {
      restrict_platform().then(response => {
        this.restrict_platform_dic = response.data
      })
    },
    // 获取应用列表
    getAppPaginateData() {
      paginate(this.search, this.page, this.page_size).then(response => {
        this.app_paginate_data = response.data.list
        this.total = response.data.totalRow
      })
    },
    // 新增编辑应用
    appEditSubmit() {
      this.app_edit_form.expire_time = this.app_edit_expire_time ? parseTime(this.app_edit_expire_time, '{y}{m}{d}{h}{i}{s}') : ''
      this.app_edit_form.user_type = this.app_edit_form.user_type.join(',')
      this.app_edit_form.restrict_platform = this.app_edit_form.restrict_platform.join(',')
      // 新增
      if (this.app_edit_form.type === 'add') {
        save(this.app_edit_form).then(response => {
          this.appEditVisible = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getAppPaginateData()
        })
      }
      // 修改
      if (this.app_edit_form.type === 'edit') {
        update(this.app_edit_form).then(response => {
          this.appEditVisible = false
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getAppPaginateData()
        })
      }
    },
    // 修改回显
    appUpdate(data) {
      this.appEditVisible = true
      this.app_edit_form.app_name = data.app_name
      this.app_edit_form.user_type = data.user_type?data.user_type.split(','):[]
      this.app_edit_form.expire_time = data.expire_time
      this.app_edit_form.add_secret = data.add_secret
      this.app_edit_form.user_token = data.user_token
      this.app_edit_form.token_expire = data.token_expire
      this.app_edit_form.restrict_access = data.restrict_access
      this.app_edit_form.restrict_platform = data.restrict_platform?data.restrict_platform.split(','):[]
      this.app_edit_expire_time = data.expire_time ? formatDateTime(data.expire_time) : ''
      this.app_edit_form.app_id_ex = data.app_id
      this.app_edit_form.type = 'edit'
    },
    // 同步至缓存
    appCache() {
      this.$confirm('确认要同步至缓存吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cache().then(response => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getAppPaginateData()
        })
      }).catch(() => {})
    },
    // 获取密钥
    getAppSecret(app_id) {
      secret(app_id).then(response => {
        this.$alert(response.data.appkey, '密钥', {confirmButtonText: '确定'});
      })
    }
  },
  created() {
    this.permissionAuth()
    this.getUserTypeDic()
    this.getRestrictPlatformDic()
    this.getAppPaginateData()
  }
}
</script>
<style lang="scss">
</style>
